<template>
    <div>
        <el-drawer v-bind="$attrs" :show-close="false" :with-header="false" :size="mobile ? '220px' : '300px'"
            @closed="close" :direction="mobile ? 'ltr' : 'rtl'" class="setting-drawer" ref="settingRef"
            modal-class="untouchable" @opened="open">
            <!-- <div class="setting-item forbid-select" v-if="!mobile">
                <div class="flex-display-center default-style title">字号：</div>
                <div style="display: flex" class="font-size-setting">
                    <div class="font-size-button default-style" @click="decrease">
                        <el-icon class="font-size-button-icon minus">
                            <component :is="getIcon('minus')" />
                        </el-icon>
                    </div>
                    <div class="flex-display-center font-size-info default-style">{{ config.fontSize }}</div>
                    <div class="font-size-button default-style" @click="increase">
                        <el-icon class="font-size-button-icon plus">
                            <component :is="getIcon('plus')" />
                        </el-icon>
                    </div>
                </div>
            </div> -->
            <!-- <div class="setting-item forbid-select">
                <div class="flex-display-center default-style label title">主题：</div>
                <el-switch v-model="config.lightTheme" @change="change" :active-action-icon="getIcon('Sunny')"
                    :inactive-action-icon="getIcon('Moon')" class="fit-auto-resize" />
            </div> -->
            <div class="setting-item forbid-select" v-if="!mobile">
                <div class="flex-display-center default-style title">泡泡拖尾：</div>
                <el-switch v-model="config.showPaoPao" active-text="开启" inactive-text="关闭" inline-prompt
                    style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                    class="forbid-select fit-auto-resize" />
            </div>
            <h5UserInfo v-if="islogin && mobile" v-bind="$attrs" />
            <div v-if="mobile" class="mobile-login">
                <div class="full-percent" style="text-align: center;padding: 10px 0;">
                    <userAvatar @click="clickAvatar" />
                </div>
                <div class="login-button-text" @click="checkLogin">{{ islogin ? '退出登录' : '登录' }}</div>
            </div>
        </el-drawer>
        <transition name="animate__animated " enter-active-class="animate__fadeInDown animate__fast "
            leave-active-class="animate__fadeOutUp animate__faster">
            <div class="login-content" v-if="showLogin" v-loading="loading">
                <div style="padding: 10px;">
                    <div style="padding: 0 0 10px 0">用户名</div>
                    <userInput v-model="loginUserInfo.username" />
                    <div style="padding: 10px 0">验证码</div>
                    <pwdBoxInput v-model="loginUserInfo.password" @complete="complete" />
                </div>
            </div>
        </transition>
        <transition name="animate__animated " enter-active-class="animate__fadeIn animate__fast "
            leave-active-class="animate__fadeOut animate__faster">
            <div class="login-mask untouchable" v-if="showLogin" @click="hideMask" />
        </transition>
    </div>
</template>

<script setup>
import { mobile, broadcastEvent } from "@/utils/tools"
import { config } from "@/utils/setting"
import router from "@/router"
import { useUserStore } from "@/pinia"
import { storeToRefs } from "pinia"
import pwdBoxInput from "../input/pwdBoxInput.vue"
import { useRequest, useLongTap } from "@usehook"
import userInput from "../input/userInput.vue"
import { useElMessage } from "@/hooks"
import h5UserInfo from "../userInfo/h5UserInfo.vue"

const userStore = useUserStore()
const { islogin } = storeToRefs(userStore)

const settingRef = $ref(null)
let showLogin = $ref(false)
let loginUserInfo = $ref({ username: "", password: "" })
watch(() => router.currentRoute.value.fullPath, () => {
    settingRef.handleClose()
    showLogin = false
})

const clickAvatar = () => {
    if (islogin.value) showUserInfo = true
}

watch(() => showLogin, () => {
    loginUserInfo = { username: "", password: "" }
})

let showUserInfo = $ref(false)

let stopLongTap = () => void 0
const open = () => {
    stopLongTap = useLongTap(document.querySelector(".login-button-text"), () => {
        if (islogin.value) {
            userStore.logout()
        }
    })
}

const close = () => {
    stopLongTap()
}

const checkLogin = () => {
    if (!islogin.value) {
        showLogin = true
    }
}

const hideMask = () => {
    if (!loading) showLogin = false
}

let loading = $ref(false)
const complete = () => {
    if (loginUserInfo.username) {
        loading = true
        useRequest.post({ url: "https://api.dansj.cn/h5_login", platform: "dansj.cn", ...loginUserInfo }).then(resp => {
            if (resp && resp.status) {
                userStore.login(resp.data)
                setTimeout(() => {
                    broadcastEvent("login", true)
                    showLogin = false
                }, 100);
            } else {
                if (resp && resp.message) useElMessage.error(resp.message)
            }
        }).catch(err => {
            useElMessage.error(err.message)
        }).finally(() => {
            loading = false
        })
    }
}
</script>

<style scoped>
.font-size-setting {
    border: 1px solid var(--border-color);
    width: fit-content;
    border-radius: var(--border-radius);
    height: 34px;
}

:deep(.font-size-button) {
    background: var(--button-color);
    padding: 5px;
}

:deep(.font-size-info) {
    width: 65px;
    height: 32px;
}

:deep(.font-size-button-icon) {
    font-size: 20px;
    color: #99a9bf;
}

:deep(.font-size-button:first-child) {
    border-right: 1px solid var(--border-color);
    border-radius: var(--border-radius) 0 0 var(--border-radius);
}

:deep(.font-size-button:last-child) {
    border-left: 1px solid var(--border-color);
    border-radius: 0 var(--border-radius) var(--border-radius) 0;
}

.setting-item {
    display: flex;
    padding: 20px 10px;
    color: var(--font-color);
}

.mobile .setting-item {
    padding: 10px 0 0 0;
}

.mobile .setting-item .label {
    width: 70px;
    justify-content: flex-end !important;
}

:deep(.el-radio__label) {
    font-weight: bold;
    font-size: 16px !important;
}

.mobile-login {
    position: absolute;
    bottom: 20px;
    left: calc(50% - 50px);
}

.login-button-text {
    width: 100px;
    background: rgb(255 255 255 / 50%);
    text-align: center;
    height: 30px;
    font-size: 18px;
    line-height: 30px;
    border-radius: 3px;
}

.login-mask {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    background: rgb(100 100 100 / 50%);
    z-index: 99999;
    overflow: hidden;
}

.login-content {
    position: fixed;
    top: calc((100% - 78px) / 4);
    left: calc(50% - 270px / 2);
    width: 250px;
    height: 146px;
    border-radius: 3px;
    background: rgb(209 209 209 / 95%);
    z-index: 999990;
    padding: 10px;
}

:deep(.userinfoModal) {
    z-index: 99999 !important;
}

:deep(.title) {
    width: 100px;
    justify-content: right !important;
    padding-right: 10px;
}

:deep(.setting-drawer .el-drawer__body) {
    padding: 5px;
}
</style>

<style>
.mobile .setting-drawer .el-drawer__body {
    padding: 5px;
}
</style>